<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" @search="search" @custom="search"></u-search></view>

		<u-tabs class="btm-tabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_02" v-for="(item, index) of newsList" :key="item.id" @click="navTo(item)">
				<view class="left">
					<view class="info-title">{{ item.title }}</view>
					<view class="info-btm">
						<view class="name clamp">{{ item.author ? item.author : item.createName }}</view>
						<text class="line"></text>
						<view class="time">{{ item.createTime | parseTime('{y}-{m}-{d}') }}</view>
					</view>
				</view>
				<view class="right"><image :src="linksImg(item.fileRepositorylist)" mode="aspectFill"></image></view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>

		<view v-if="false && news && dict"></view>
	</view>
</template>

<script>
import { mapState } from 'vuex';
export default {
	data() {
		return {
			scrollTop: 0,
			tabsCurrent: 0,
			tabsList: [],
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b'
			},
			classification: -1,
			title: '在线学习',
			dict: 'online_study_type',
			newsList: [],

			news: {
				classification: '',
				clientCode: '370702',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.dict) {
			this.dict = option.dict;
		}
		if (this.dict == 'sxzx_article_classification') {
			this.title = '书香人大';
		}

		uni.setNavigationBarTitle({
			title: this.title
		});
		this.loadDict(this.dict);
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.news.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
			} else {
				this.clientCode = '370702';
				// this.$logout();
			}
			this.pageNum = 1;
			this.pageSize = 10;
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add' && this.loading == 'nomore') {
				return false;
			}

			this.loading = 'loading';
			if (type != 'add') {
				this.pageNum = 1;
			}
			if (this.news.classification == '-1') {
				this.news.classification = '';
			}
			let url = '/olstudy/content/list';
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				typeSea: this.news.classification,
				clientCode: this.news.clientCode,
				titleSea: this.news.title,
				userId: this.userId
			};

			if (this.dict == 'sxzx_article_classification') {
				url = '/shuxiangzhengxie/scholarly/list';
				data = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					scholarly: this.news
				};
			}
			this.$request(url, data, {
				load: true
			}).then(res => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.newsList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.newsList = this.newsList.concat(res.data.records);
				this.loaded = true;
				if (this.newsList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDict(type) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then(res => {
				this.tabsList.push({
					name: '所有',
					value: '-1'
				});
				res.data.forEach(item => {
					this.tabsList.push({
						name: item.dictName,
						value: item.dictValue
					});
				});
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.type(this.tabsList[index].value);
		},
		search(e) {
			this.news.title = e;
			this.loadData('refresh');
		},
		type(e) {
			this.classification = e;
			this.news.classification = e.toString();
			this.loadData('refresh');
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pages/bbq/detail?id=' + item.id + '&dict=' + this.dict
			});
		},
		linksImg(item) {
			if (item && item.length > 0) {
				return this.$baseUrl + '/file/download/preview/0?path=' + item[0].path.replace(/\\/g, '/');
			} else {
				if (this.dict == 'sxzx_article_classification') {
					return this.$staticImgRandom('/static/img/default_big.png');
				}
				return this.$staticImgRandom('/static/img/default_bg01.png');
			}
		}
	}
};
</script>

<style lang="scss">
.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.list-info_02 {
	.time {
		flex-shrink: 0;
	}

	.right {
		overflow: hidden;

		image {
			width: 236rpx;
			height: 148rpx;
		}
	}
}
</style>
